﻿/*
 DotNetNuke - http://www.dotnetnuke.com
 Copyright (c) 2002-2010 by DotNetNuke Corp.

 Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated 
 documentation files (the "Software"), to deal in the Software without restriction, including without limitation 
 the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and 
 to permit persons to whom the Software is furnished to do so, subject to the following conditions:

 The above copyright notice and this permission notice shall be included in all copies or substantial portions 
 of the Software.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED 
 TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL 
 THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF 
 CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER 
 DEALINGS IN THE SOFTWARE.

*/
using System;
using System.Data;

using DotNetNuke.Common.Utilities;
using DotNetNuke.Framework.Providers;
using Microsoft.ApplicationBlocks.Data;


namespace DotNetNuke.Modules.UserDefinedTable
{


  public class SqlDataProvider : DataProvider
  {



    private string _connectionString;
    private string _databaseOwner;
    private string _objectQualifier;
    private ProviderConfiguration _providerConfiguration = ProviderConfiguration.GetProviderConfiguration("data");
    private string _providerPath;
    private const string ProviderType = "data";
    private const string SPPrefix = "UserDefinedTable_";




    public SqlDataProvider()
    {
      Provider provider = (Provider)this._providerConfiguration.Providers[this._providerConfiguration.DefaultProvider];
      this._connectionString = Config.GetConnectionString();

      if (this._connectionString == "")
        this._connectionString = provider.Attributes["connectionString"];

      this._providerPath = provider.Attributes["providerPath"];
      this._objectQualifier = provider.Attributes["objectQualifier"];

      if ((this._objectQualifier != "") & !this._objectQualifier.EndsWith("_"))
        this._objectQualifier = this._objectQualifier + "_";

      this._databaseOwner = provider.Attributes["databaseOwner"];

      if ((this._databaseOwner != "") & !this._databaseOwner.EndsWith("."))
        this._databaseOwner = this._databaseOwner + ".";

    }




    public override void AddData(int UserDefinedRowId, int UserDefinedFieldId, string FieldValue)
    {
      SqlHelper.ExecuteNonQuery(
        this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_AddData"
        , UserDefinedRowId, UserDefinedFieldId, FieldValue
      );
    }




    public override int AddField(
      int ModuleId, string FieldTitle, int BeforePos
      , string HelpText, bool Required, string FieldType
      , string Default, bool Visible, bool ShowOnEdit
      , bool Searchable, bool IsPrivateColumn, string InputSettings
      , string OutputSettings, bool NormalizeFlag, string validationRule
      , string validationMessage, string EditStyle
    )
    {

      return Convert.ToInt32(SqlHelper.ExecuteScalar(this.ConnectionString
          , this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_AddField"
          , ModuleId, FieldTitle, this.GetNull(BeforePos), this.GetNull(HelpText), Required, FieldType, this.GetNull(Default)
          , Visible, ShowOnEdit, Searchable, IsPrivateColumn, this.GetNull(InputSettings), this.GetNull(OutputSettings)
          , NormalizeFlag, this.GetNull(validationRule), this.GetNull(validationMessage)
          , this.GetNull(EditStyle)
      ));

    }




    public override int AddRow(int ModuleId)
    {
      return Convert.ToInt32(SqlHelper.ExecuteScalar(
        this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_AddRow"
        , ModuleId
      ));
    }




    public override void DeleteData(int UserDefinedRowId, int UserDefinedFieldId)
    {
      SqlHelper.ExecuteNonQuery(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_DeleteData"
        , UserDefinedRowId, UserDefinedFieldId
      );
    }




    public override void DeleteField(int UserDefinedFieldId)
    {
      SqlHelper.ExecuteNonQuery(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_DeleteField"
        , UserDefinedFieldId 
      );
    }




    public override void DeleteRow(int UserDefinedRowId, int ModuleId)
    {
      SqlHelper.ExecuteNonQuery(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_DeleteRow"
        , UserDefinedRowId, ModuleId
      );
    }




    public override void DeleteRows(int ModuleId)
    {
      SqlHelper.ExecuteNonQuery(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_DeleteRows"
        , ModuleId
      );
    }




    public override void FillDefaultData(int ModuleId, int FieldId, string DefaultValue)
    {
      SqlHelper.ExecuteNonQuery(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_FillDefaultData"
        , ModuleId, (this.GetNull(FieldId)), DefaultValue
      );
    }




    public override IDataReader GetData(int UserDefinedRowId, int UserDefinedFieldId)
    {
      return SqlHelper.ExecuteReader(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_GetData"
        , UserDefinedRowId, UserDefinedFieldId
      );
    }




    public override IDataReader GetField(int UserDefinedFieldId)
    {
      return SqlHelper.ExecuteReader(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_GetField"
        , UserDefinedFieldId
      );
    }





    public override int GetFieldDataCount(int UserDefinedFieldID)
    {
      return Convert.ToInt32(SqlHelper.ExecuteScalar(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_GetFieldDataCount"
        , UserDefinedFieldID
      ));
    }




    public override IDataReader GetFields(int ModuleId)
    {
      return SqlHelper.ExecuteReader(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_GetFields"
        , new object[] { ModuleId });
    }




    public override int GetMaxFieldSize()
    {
      return Null.NullInteger;
    }




    private object GetNull(object Field)
    {
      return Null.GetNull((Field), DBNull.Value);
    }




    public override IDataReader GetRow(int UserDefinedRowId, int ModuleId)
    {
      return SqlHelper.ExecuteReader(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_GetRow"
        , UserDefinedRowId, ModuleId
      );
    }




    public override IDataReader GetRows(int ModuleId)
    {
      return SqlHelper.ExecuteReader(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_GetRows"
        , ModuleId
      );
    }




    public override void Reset(int ModuleId)
    {
      SqlHelper.ExecuteNonQuery(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_Reset"
        , ModuleId
      );
    }




    public override void SwapFieldOrder(int FirstUserDefinedFieldId, int SecondUserDefinedFieldId)
    {
      SqlHelper.ExecuteNonQuery(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_SwapFieldOrder"
        , FirstUserDefinedFieldId, SecondUserDefinedFieldId
      );
    }




    public override void UpdateData(int UserDefinedRowId, int UserDefinedFieldId, string FieldValue)
    {
      SqlHelper.ExecuteNonQuery(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_UpdateData"
        , UserDefinedRowId, UserDefinedFieldId, FieldValue
      );
    }




    public override void UpdateField(int UserDefinedFieldId, string FieldTitle, string HelpText
      , bool Required, string FieldType, string Default
      , bool Visible, bool ShowOnEdit, bool Searchable
      , bool IsPrivateColumn, string InputSettings
      , string OutputSettings, bool NormalizeFlag
      , string validationRule, string validationMessage
      , string EditStyle
    )
    {
      SqlHelper.ExecuteNonQuery(this.ConnectionString, this.DatabaseOwner + this.ObjectQualifier + "UserDefinedTable_UpdateField"
        , UserDefinedFieldId, FieldTitle, (this.GetNull(HelpText)), Required, FieldType
        , this.GetNull(Default), Visible, ShowOnEdit, Searchable, IsPrivateColumn
        , (this.GetNull(InputSettings)), (this.GetNull(OutputSettings)), NormalizeFlag
        , this.GetNull(validationRule), this.GetNull(validationMessage), this.GetNull(EditStyle)
      );
    }




    public string ConnectionString
    { get { return this._connectionString; } }




    public string DatabaseOwner
    { get { return this._databaseOwner; } }




    public string ObjectQualifier
    { get { return this._objectQualifier; } }




    public string ProviderPath
    { get { return this._providerPath; } }



  }

}

